package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.List;
import java.util.Map;

import objects.JCJGDetail;

/**
 * Created by Monkey on 2017/3/21.
 * 检查结果适配器
 */

public class JCJGListAdapter extends BaseExpandableListAdapter {

    private Map<String, List<JCJGDetail>> dataSet;
    private Context mContext;
    private LayoutInflater inflater;
    private String[] parents = {};

    public JCJGListAdapter(Context context) {
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
        this.parents = mContext.getResources().getStringArray(R.array.pacs_type);
    }

    public void setDataSet(Map<String, List<JCJGDetail>> dataSet) {
        this.dataSet = dataSet;
    }

    @Override
    public int getGroupCount() {
        if (dataSet != null && dataSet.size() > 0) {
            return dataSet.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int i) {
        if (dataSet.get(parents[i]) != null && dataSet.get(parents[i]).size() > 0) {
            return dataSet.get(parents[i]).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return parents[i];
//        return null;
    }

    @Override
    public Object getChild(int parentPos, int childPos) {
        if (dataSet.get(parents[parentPos]) != null && dataSet.get(parents[parentPos]).get(childPos) != null) {
            return dataSet.get(parents[parentPos]).get(childPos);
        }
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int parentPos, boolean b, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_jcjg_parent, parent, false);
            holder.textView = (TextView) convertView.findViewById(R.id.jc_jcfl_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        holder.textView.setText(parents[parentPos]);
        return convertView;
    }

    @Override
    public View getChildView(int parentPos, int childPos, boolean b, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        List<JCJGDetail> list = dataSet.get(parents[parentPos]);
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_jcjg_child, parent, false);
            holder.jc_jcxm = (TextView) convertView.findViewById(R.id.jc_jcxm_text);
            holder.jc_jcrq = (TextView) convertView.findViewById(R.id.jc_jcrq_text);
            holder.jc_jcbw = (TextView) convertView.findViewById(R.id.jc_jcbw_text);
            holder.jc_jclx = (TextView) convertView.findViewById(R.id.jc_jclx_text);
            holder.jc_sqys = (TextView) convertView.findViewById(R.id.jc_sqys_text);
            holder.jc_sqrq = (TextView) convertView.findViewById(R.id.jc_sqrq_text);
            holder.jc_bgys = (TextView) convertView.findViewById(R.id.jc_bgys_text);
            holder.jc_bgrq = (TextView) convertView.findViewById(R.id.jc_bgrq_text);
            holder.jc_jczd = (TextView) convertView.findViewById(R.id.jc_jczd_text);
            holder.jc_jcms = (TextView) convertView.findViewById(R.id.jc_jcms_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            JCJGDetail jcjgDetail = list.get(childPos);
            if (jcjgDetail.getJCXM() != null) {
                holder.jc_jcxm.setText(jcjgDetail.getJCXM());
            }
            if (jcjgDetail.getJCSJ() != null) {
                holder.jc_jcrq.setText(jcjgDetail.getJCSJ().substring(0, 10));
            }
            if (jcjgDetail.getJCBW() != null) {
                holder.jc_jcbw.setText(jcjgDetail.getJCBW());
            }
            if (jcjgDetail.getJCLX() != null) {
                holder.jc_jclx.setText(jcjgDetail.getJCLX());
            }
            if (jcjgDetail.getSQYS() != null) {
                holder.jc_sqys.setText(jcjgDetail.getSQYS());
            }
            if (jcjgDetail.getSQSJ() != null) {
                holder.jc_sqrq.setText(jcjgDetail.getSQSJ().substring(0, 10));
            }
            if (jcjgDetail.getBGYS() != null) {
                holder.jc_bgys.setText(jcjgDetail.getBGYS());
            }
            if (jcjgDetail.getBGSJ() != null) {
                holder.jc_bgrq.setText(jcjgDetail.getBGSJ().substring(0, 10));
            }
            if (jcjgDetail.getJCZD() != null) {
                holder.jc_jczd.setText(jcjgDetail.getJCZD());
            }
            if (jcjgDetail.getJCMS() != null) {
                holder.jc_jcms.setText(jcjgDetail.getJCMS());
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    private class ParentViewHolder {
        TextView textView;
    }

    private class ChildViewHolder {
        TextView jc_jcxm;
        TextView jc_jcrq;
        TextView jc_jcbw;
        TextView jc_jclx;
        TextView jc_sqys;
        TextView jc_sqrq;
        TextView jc_bgys;
        TextView jc_bgrq;
        TextView jc_jczd;
        TextView jc_jcms;
    }
}
